package com.chen.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.system.config.RequestDynamicTableNameHelper;
import com.chen.system.entity.DynamicDemoDO;
import com.chen.system.mapper.DynamicDemoMapper;
import com.chen.system.service.DynamicDemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DynamicDemoServiceImpl extends ServiceImpl<DynamicDemoMapper, DynamicDemoDO> implements DynamicDemoService {

    @Autowired
    private DynamicDemoMapper dynamicDemoMapper;

    @Override
    public void save(String dynamicTableName, DynamicDemoDO dynamicDemoDO) {
        //将动态表名放到请求参数中（表名）
        if(StringUtils.isNotBlank(dynamicTableName)){
            RequestDynamicTableNameHelper.setRequestData(dynamicTableName);
        }
        //和以前使用一样
        dynamicDemoMapper.insert(dynamicDemoDO);
    }

    @Override
    public void updateById(String dynamicTableName, DynamicDemoDO dynamicDemoDO) {
        if(StringUtils.isNotBlank(dynamicTableName)){
            RequestDynamicTableNameHelper.setRequestData(dynamicTableName);
        }
        dynamicDemoMapper.updateById(dynamicDemoDO);
    }

    @Override
    public void deleteById(String dynamicTableName, Long id) {
        if(StringUtils.isNotBlank(dynamicTableName)){
            RequestDynamicTableNameHelper.setRequestData(dynamicTableName);
        }
        dynamicDemoMapper.deleteById(id);

    }

    @Override
    public DynamicDemoDO getById(String dynamicTableName, Long id) {
        if(StringUtils.isNotBlank(dynamicTableName)){
            RequestDynamicTableNameHelper.setRequestData(dynamicTableName);
        }
        return dynamicDemoMapper.selectById(id);
    }
}
